<?php
$yy_debug_global_count = 0;

if (!function_exists('yy_debug_space_line')) {
    function yy_debug_space_line()
    {
        echo '<br/><br/>';
    }
}

if (!function_exists('yy_debug_next_line')) {
    function yy_debug_next_line()
    {
        echo '<br/>';
    }
}

if (!function_exists('yy_debug_start_html')) {
    function yy_debug_start_html()
    {
        echo '<html><head><title>php-simple-debug</title></head><body><pre>';
    }
}

if (!function_exists('yy_debug_end_html')) {
    function yy_debug_end_html()
    {
        echo '</pre></body></html>';
    }
}

if (!function_exists('yy_debug_show')) {
    function yy_debug_show($var, $enable_json)
    {
        if (is_null($var)) {
            echo '...';
        } else if (is_array($var) || is_object($var)) {
            if ($enable_json) {
                echo json_encode($var, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
            } else {
                print_r($var);
            }
        } else if (is_string($var) || is_numeric($var)) {
            echo $var;
        } else {
            var_dump($var);
        }

        yy_debug_next_line();
    }
}

if (!function_exists('yy_debug_info')) {
    function yy_debug_info()
    {
        echo '<b><i>';

        $trace = debug_backtrace();
        if (isset($trace[2]['file'])) {
            echo 'file: ' . $trace[2]['file'] . '    ';
        } else {
            if (isset($trace[1]['file'])) {
                echo 'file: ' . $trace[1]['file'] . '    ';
            }
        }
        if (isset($trace[2]['class'])) {
            echo 'class: ' . $trace[2]['class'] . '    ';
        }
        if (isset($trace[2]['function'])) {
            echo 'function: ' . $trace[2]['function'] . '    ';
        }
        if (isset($trace[2]['line'])) {
            echo 'line: ' . $trace[2]['line'] . '    ';
        } else {
            if (isset($trace[1]['line'])) {
                echo 'line: ' . $trace[1]['line'] . '    ';
            }
        }

        echo '</i></b>';

        yy_debug_space_line();
    }
}

if (!function_exists('yy')) {
    function yy($var = null, $enable_json = true)
    {
        yy_debug_start_html();
        yy_debug_info();
        yy_debug_show($var, $enable_json);
        yy_debug_end_html();
        exit(1);
    }
}

if (!function_exists('tt')) {
    function tt($var = null)
    {
        yy($var, false);
    }
}

if (!function_exists('rr')) {
    function rr($var = null, $count = 1, $enable_json = true)
    {
        global $yy_debug_global_count;

        if ($yy_debug_global_count == 0) {
            yy_debug_start_html();
        }

        yy_debug_info();
        yy_debug_show($var, $enable_json);
        yy_debug_space_line();

        if (++$yy_debug_global_count >= $count) {
            yy_debug_end_html();
            exit(1);
        }
    }
}

if (!function_exists('ee')) {
    function ee($var = null, $count = 1)
    {
        rr($var, $count, false);
    }
}

